<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      @click-left="$router.back()"
      left-arrow
      title="重置密码"
    />
    <!-- /导航栏 -->
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" :show-error="false" ref="form">
      <van-field
        v-model="user.mobile"
        name="mobile"
        label="手机号"
        placeholder="请输入手机号"
        type="number"
        :rules="rules.mobile"
        clearable
      >
      </van-field>

      <!-- 验证码 -->
      <van-field
        v-model="user.code"
        name="code"
        label="验证码"
        placeholder="请输入验证码"
        type="number"
        :rules="rules.code"
        clearable
      >
        <template #button>
          <van-button size="small" v-if="isShow">
            <van-count-down
              :time="1000 * 3"
              format=" ss 秒"
              @finish="isShow = false"
            />
          </van-button>
          <van-button v-else @click="sendSms" size="small" type="primary"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <van-field
        v-model="user.password"
        name="password"
        label="新密码"
        placeholder="请输入新密码"
        :rules="rules.password"
        clearable
      >
      </van-field>
      <div class="login-btn-wrap">
        <van-button
          class="login-btn"
          round
          block
          type="info"
          native-type="submit"
          @click="resetPassword"
          >立即修改</van-button
        >
      </div>
    </van-form>
    <!-- /登录表单 -->
  </div>
</template>

<script>
import { getSms, editUser } from "@/api/cuixingkui/user.js";
export default {
  name: "Reset",
  data() {
    return {
      isShow: false,
      user: {
        mobile: "13800000005",
        password: "",
        code: "888888	",
      },
      rules: {
        mobile: [
          { required: true, trigger: blur, message: "请输入手机号" },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: "手机号格式错误",
          },
        ],
        password: [
          { required: true, trigger: blur, message: "请输入新密码" },
          {
            pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/,
            message: "密码至少包含 数字和英文，长度6-20",
          },
        ],
        code: [
          { required: true, trigger: blur, message: "请输入验证码" },
          { pattern: /^\d{6}$/, message: "请输入6位验证码" },
        ],
      },
    };
  },
  methods: {
    onSubmit(val) {
      console.log(val);
    },
    //发送短信
    async sendSms() {
      //验证手机号是否正确
      try {
        await this.$refs.form.validate("mobile");
      } catch (error) {
        console.log(error);
        return this.$toast.fail("手机号格式不正确");
      }
      //发送请求
      try {
        await getSms(this.user.mobile);
        this.$toast.success("发送成功！请查收");
      } catch (error) {
        console.log(error);
        return this.$toast.fail("验证码发送失败");
      }
      //显示倒计时
      this.isShow = true;
    },
    //重置密码
    async resetPassword(val) {
      console.log(val);
      //获取到表单数据
      let { data } = await editUser(this.user);
      console.log(data);
      if (data.errno == 1000) {
        return this.$toast.fail("密码不能为空!");
      } else if (data.errno !== 0) {
        return this.$toast.fail("密码修改失败！");
      } else {
        this.$toast.success("修改成功！");
        this.$router.push("/login");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login-btn {
  width: 650px;
  margin-left: 50px;
}
</style>
